package edu.gatech.cs2340.group7.wheresmystuffapp.model;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is responsible for manipulating, organizing, and sorting the items.
 * @author tbowling3
 *
 */
public class ItemManager {

	/**
	 * Returns all the lost items for all the users in the system
	 * @return All the Lost items for all users in the system
	 */
	public List<Item> getLostItems(List<Item> list) {
		List<Item> newList = list;
		for(Item i: list){
			if(!i.isLostItem()){
				newList.remove(i);
			}
		}
		return newList;
	}
	
	/**
	 * Returns all the Found items for all the users in the system
	 * @return All the Found items for all users in the system
	 */
	public List<Item> getFoundItems(List<Item> list) {
		
		List<Item> newList = list;
		for(Item i: list){
			if(i.isLostItem()){
				newList.remove(i);
			}
		}
		return newList;
	}
	
	/**
	 * When provided a list of items, this method will return a list of
	 * only the items that are unresolved.
	 * @param list List of items to be filtered
	 * @return List of unresolved items
	 */
	public List<Item> filterUnresolvedItems(List<Item> list) {
		return list;
	}
	
	/**
	 * When provided a list of items, this method will return a list of
	 * only the items that are resolved.
	 * @param list List of items to be filtered
	 * @return List of resolved items
	 */
	public List<Item> filterResolvedItems(List<Item> list) {
		return list;
	}
	
	/**
	 * Filters the list of items and returns the items which are of the provided category
	 * @param list List of items to filter
	 * @param category Category to use for filter
	 * @return List of items of the given category
	 */
	public List<Item> filterItemsByCategory(List<Item> list, String category) {
		ArrayList<Item> newList = new ArrayList<Item>();
		for(Item i :list){
			if (i.getCategory().equals(category)){
				newList.add(i);
			}
		}
		return newList;
	}
	
	/**
	 * 
	 * @param list The list of Items
	 * @param d The date to filter by
	 * @return A list of items that were 'reported' after a specific date
	 */
	public List<Item> filterItemsbyLostDate(List<Item> list, CustomDate d) {
		ArrayList<Item> newList = new ArrayList<Item>();
		for(Item i: list){
			if (i.getDateEntered().after(d)){
				newList.add(i);
			}
		}
		return newList;
	}
	
	/**
	 * 
	 * @param list List of Items 
	 * @return list
	 */
	public List<Item> resetFilterItems(List<Item> list){
		return list;
	}
	
	/**
	 * Filters only lost items
	 * @param list complete list of items that the current user has registered
	 * @return a list that contains only the lost items registered by a current user
	 */
	public List<Item> filterLostItems(List<Item> list){
		List<Item> newList = new ArrayList<Item>();
		for(Item i: list){
			if(i.isLostItem()){
				newList.add(i);
			}
		}
		return newList;
	}
	
	/**
	 * Filters only found Items
	 * @param list -complete list of items that the current user has registered
	 * @return a list that contains only the found items registered by the current user
	 */
	public List<Item> filterFoundItems(List<Item> list){
		List<Item> newList = new ArrayList<Item>();
		for(Item i: list){
			if(!i.isLostItem()){
				newList.add(i);
			}
		}
		return newList;
	}
	
	/**
	 * Filters items that belong to the current account
	 * @param list -the complete list of items that have been registered
	 * @return a list of items that only the current user has registered
	 */
	public List<Item> filterByAccount(List<Item> list){
		List<Item> newList = new ArrayList<Item>();
		String username = DataManager.getCurrentAccountName();
		for(Item i:list){
			if(i.getUsername().equals(username)){
				newList.add(i);
			}
		}
		return newList;
		
	}
	
	/**
	 * Filters the list of items by a specific name -- used when searching for an item
	 * @param list the complete list of items that have been registered
	 * @param userInput The name that the user wants to search for
	 * @return the list of items whose names match the user's input
	 */
	public List<Item> filterByName(List<Item> list, String userInput){
		List<Item> newList = new ArrayList<Item>();
		for (Item i : list){
			if (i.getName().equalsIgnoreCase(userInput)){
				newList.add(i);
			}
		}
		return newList;
	}
	
}
